import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';

type MessageInfoType = {
  type: 'success' | 'error' | 'info' | 'warning';
  content?: string;
};

interface MessageState {
  messageInfo: MessageInfoType;
}

// 初始值
const initialState: MessageState = {
  messageInfo: {
    type: 'success',
  },
};

// 创建切片
const messageSlice = createSlice({
  name: 'message',
  initialState,
  reducers: {
    saveMessageInfo: (state, action: PayloadAction<MessageInfoType>) => {
      state.messageInfo = { ...action.payload };
    },
  },
});

// 用于redux注册的
export default messageSlice.reducer;

// @reduxjs/toolkit会自动生成action，外部调用这个就行
export const { saveMessageInfo } = messageSlice.actions;
